/*
 * @Author: ZhaoZhiqi
 * @Date: 2024-10-17 17:35:57
 * @LastEditors: ZhaoZhiqi
 * @LastEditTime: 2024-10-17 18:16:48
 * @Description: 工具函数
 * @FilePath: \charging-mini\充电项目\src\libs\utils.js
 */
import moment from "moment";
/**
 * @description: 格式化充电时长
 * @param {Number} chargingTime 充电时长
 * @return {*}
 */
export const formatChargingTime = (chargingTime) => {
    let days = moment.duration(chargingTime).days();
    let hours = moment.duration(chargingTime).hours();
    let minutes = moment.duration(chargingTime).minutes();
    let str = "";
    if (days > 0) {
        str += `${days}天`;
    }
    if (hours > 0 || days > 0) {
        str += `${hours}小时`;
    }
    if (minutes > 0 || hours > 0 || days > 0) {
        str += `${minutes}分钟`;
    }
    if (!str) {
        str = "小于1分钟";
    }
    return str;
};

/**
 * @description: 获取http链接中的参数
 * @param {String} url url
 * @param {String} key 要提取的参数名
 * @return {String}
 */
export const getQueryString = (url, key) => {
    let urlArr = url.split("?");
    if (!urlArr[1]) return "";
    var theRequest = {};
    var strs = urlArr[1].split("&");
    for (var i = 0; i < strs.length; i++) {
        theRequest[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
    }
    return theRequest[key] == null ? "" : theRequest[key];
};